﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SudokuGame.Logic;

namespace SudokuGame.Interface
{
    public partial class Form1 : Form
    {
        private Graphics g, g2;
        private SudokuController su;
        private String adress;
        public enum Direction{
            Left, Right, Up, Down
        }
        public Form1()
        {
            InitializeComponent();
            
            this.Focus();
            this.KeyPreview = true;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            su.Shuffle();
            this.Focus();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            Dialog dialog = new Dialog();
            DialogResult x = dialog.ShowDialog();
            switch (x)
            {
                case DialogResult.OK:
                    {
                        BufferedGraphicsContext bgc = new BufferedGraphicsContext();
                        g = pictureBox1.CreateGraphics();
                        BufferedGraphics _g = bgc.Allocate(g, pictureBox1.Bounds);
                        g2 = pictureBox2.CreateGraphics();
                        BufferedGraphics _g2 = bgc.Allocate(g2, pictureBox1.Bounds);
                        adress = dialog.adress;
                        su = new SudokuController(adress, ref _g, ref _g2);
                        break;
                    }
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            su.changeCurrent(e.X, e.Y);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (Char.IsDigit(Convert.ToChar(e.KeyCode))) 
            {
                bool solved = su.addToSolution(Convert.ToInt32(Convert.ToString( Convert.ToChar(e.KeyCode))));
                if (solved) MessageBox.Show("You win!");
            
            }
            
            Console.WriteLine(e.KeyCode.ToString());
            
        }

       /* private void button1_KeyDown(object sender, KeyEventArgs e)
        {
            Form1_KeyDown(sender, e);
        }
        */
   /*     protected override bool IsInputKey(System.Windows.Forms.Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Up:
                    OnKeyDown(new KeyEventArgs(Keys.Up));
                    return true;
                case Keys.Down:
                    return true;
                case Keys.Right:
                    return true;
                case Keys.Left:
                    return true;
                default:
                    return base.IsInputKey(keyData);
            }

        }*/



        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            Console.WriteLine(keyData);
            switch (keyData)
            {

                case Keys.Delete:
                    {
                        su.removeFromSolution();
                        break;
                    }
                case Keys.Up:
                    {
                        su.changeCurrent(Direction.Up);
                        break;
                    }
                case Keys.Down:
                    {
                        su.changeCurrent(Direction.Down);
                        break;
                    }
                case Keys.Left:
                    {
                        su.changeCurrent(Direction.Left);
                        break;
                    }
                case Keys.Right:
                    {
                        su.changeCurrent(Direction.Right);
                        break;
                    }

            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        
       
    }
}
